//
//  LYCountDownButton.m
//  LYCountDownButton
//
//  Created by VierGhost on 15/7/24.
//  Copyright (c) 2015年 dingli. All rights reserved.
//

#import "LYCountDownButton.h"
#import "UIColor+LYString.h"

@interface LYCountDownButton () {
    int _second;
    
    NSTimer *_timer;
    NSDate *_startDate;
}

@end

@implementation LYCountDownButton
+ (LYCountDownButton *)countDownButton {
    LYCountDownButton *btn = [LYCountDownButton buttonWithType:UIButtonTypeCustom];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitle:@"免费获取" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    btn.layer.cornerRadius = 2;
    btn.clipsToBounds = YES;
    return btn;
}


+ (LYCountDownButton *)setButtonType:(UIButtonType *)type {
    LYCountDownButton *btn = [LYCountDownButton buttonWithType:UIButtonTypeCustom];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitle:@"免费获取" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHexString:@"#FB6F00"] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithHexString:@"#FFFFFF"]];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 2;
    btn.layer.borderWidth = 0.5;
    btn.layer.borderColor = [[UIColor colorWithHexString:@"#FB6F00"] CGColor];
    
    return btn;
}

#pragma -mark touche action
- (void)addToucheHandler:(TouchedDownBlock)touchHandler{
    _touchedDownBlock = [touchHandler copy];
    if (_touchedDownBlock) {
        [self addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)touched:(LYCountDownButton *)sender{
    if (_touchedDownBlock) {
        _touchedDownBlock(sender,sender.tag);
    }
}

#pragma -mark count down method
- (void)startWithSecond:(int)totalSecond {
    _totalSecond = totalSecond;
    _second = totalSecond;
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
        }
        _timer = nil;
    }
    
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    _startDate = [NSDate date];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)timerStart:(NSTimer *)theTimer {
     double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
     _second = _totalSecond - (int)(deltaTime + 0.5);
    if (_second < 0.0) {
        [self stop];
    } else {
        NSString *title;
        if (_didChangeBlock) {
            title = _didChangeBlock(self, _totalSecond);
        } else {
            title = [NSString stringWithFormat:@"剩余%d秒",_second];
        }
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateDisabled];
    }
}

- (void)stop{
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
            _timer = nil;
            _second = _totalSecond;
            NSString *title;
            if (_didFinishedBlock) {
                title = _didFinishedBlock(self, _totalSecond);
            } else {
                title = @"重新获取";
            }
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitle:title forState:UIControlStateDisabled];
        } else {
            _timer = nil;
        }
    }
}

#pragma -mark block
- (void)didChange:(DidChangeBlock)didChangeBlock{
    _didChangeBlock = [didChangeBlock copy];
}

- (void)didFinished:(DidFinishedBlock)didFinishedBlock{
    _didFinishedBlock = [didFinishedBlock copy];
}
@end
